duolingofandomcom_pt-20200213-history
Módulo:Utility
This module includes useful functions --local myFrame = { args = {'es','en', '3','n','n','n','2011-06-29','2012-06-19','2012-09-06','!0 22 22 44!' }} --p.test(myFrame) local p = {} local sVarName ='data' -- Use this for testing functions function p.test(frame) -- mw.log('Running tests' ) local b = p.exists('d',false) if not(b) then p.defineVar('d',0,false) end local t = p.getVar('d',false) t = tonumber(t) + 1 p.defineVar('d',t,false) --[[ --local test =p.getArgs(frame)1 --local t = {'s'=1} --local test = p.isEmpty(t) --p.main() --local t = getArgs(frame) -- test = test .. " ; value : 6 -> ".. t6 --]] return t end function p.testdef() mw.log('Running tests' ) --local t = p.checkArgs(frame,6) --p.define(sVarName,'t',false) --local t = p.exists(sVarName,false)--p.defineVar('d',90,false) return t end function p.getFrame(bParent) local oFrame = mw.getCurrentFrame() if bParent and oFrame~=nil and oFrame:getParent() then oFrame = mw.getCurrentFrame():getParent() end return oFrame end -- Put all arguments into a real table function p.getArgs(frame,bParent) local t = {} local tTmp = frame.args if bParent and frame and frame:getParent() then tTmp = frame:getParent().args end for name, value in pairs( tTmp ) do tname = value end return t end --Check if args are enough for script to run function p.checkArgs(frame, iArgs,bParent) local iCount =0 local tTmp = frame.args if bParent then tTmp = frame:getParent().args end for name, value in pairs( tTmp ) do iCount = iCount + 1 if (iCount) tonumber(iArgs) then mw.log('true') return true end end mw.log("false") return false end --Check if date is in appropriate format function p.checkDate(date) local pat="(%d%d%d%d)-(%d%d)-(%d%d)" if date then local year,month,day=date:match(pat) if year and month and day then if (tonumber(month) < 13 and tonumber(day) < 32 and not(tonumber(year)< 0)) then return true end end end return false end -- Check if table is empty function p.isEmpty(table) if next(table) nil then return true end return false end -- round a number function p.round(num, idp) return tonumber(string.format("%." .. (idp or 0) .. "f", num)) end -- Calculate difference between two dates function p.datediff( date1, date2) if p.checkDate(date1) and p.checkDate(date2) then date1 = string.sub(date1,0,10) date2 = string.sub(date2,0,10) local tDate1 =p.getTime(date1) local tDate2 =p.getTime(date2) local days = math.abs((os.difftime(tDate1,tDate2))/3600/24) return days end return 0 end --Get time using Iso format ((YYYY-MM-DD) function p.getTime(date) local pat="(%d%d%d%d)-(%d%d)-(%d%d)" local year,month,day=date:match(pat) local date1=(os.time({month=month,year=year,day=day}) ) return date1 end -- --Get time using Iso format ((YYYY-MM-DD) function p.getdate(date,format) if not(format) then format = "%Y-%m-%d" end if not(date) then date = os.date("%Y-%m-%d") end local pat="(%d%d%d%d)-(%d%d)-(%d%d)" local year,month,day=date:match(pat) local date1=(os.time({month=month,year=year,day=day}) ) local sDate =os.date(format,date1) return sDate end -- --Get time using custom date function p.date(frame) local tTable =p.getArgs(frame,false) if next(tTable) 0 then tTable =p.getArgs(frame,true) end local sDate = (p.getdate(tTable1,tTable2)) return sDate end -- check if Variable exists function p.exists( varname, bParent ) if not varname then return false end local oFrame = p.getFrame(bParent) local res if oFrame then res = tostring( oFrame:preprocess(' ') ) end return res '1' and true or false end --Get Parent of calling page/parent. This requires (Extension:Variables) function p.getVar(sVariable,bParent) if not(sVariable) then return end local oFrame=p.getFrame(bParent) local sVar if (oFrame~=nil) then sVar = tostring( mw.getCurrentFrame():preprocess(' ')) end return sVar end --Define variable of calling page/parent. (Extension:Variables) function p.defineVar(sVariable, sNewValue, bParent) sVariable = tostring(sVariable) sNewValue = tostring(sNewValue) if not(sVariable) or not(sNewValue) then return end local oFrame=p.getFrame(bParent) if (oFrame~=nil) then oFrame:preprocess(' ') end end --Source: http://help.interfaceware.com/kb/112 --Saves table function p.savetable(Table) local savedTables = {} -- used to record tables that have been saved, so that we do not go into an infinite recursion local outFuncs = { 'string' = function(value) return string.format("%q",value) end; 'boolean' = function(value) if (value) then return 'true' else return 'false' end end; 'number' = function(value) return string.format('%f',value) end; } local outFuncsMeta = { __index = function(t,k) error('Invalid Type For SaveTable: '..k) end } setmetatable(outFuncs,outFuncsMeta) local tableOut = function(value) if (savedTablesvalue) then error('There is a cyclical reference (table value referencing another table value) in this set.'); end local outValue = function(value) return outFuncstype(value)(value) end local out = '{' for i,v in pairs(value) do out = out..''..outValue(i)..'='..outValue(v)..';' end savedTablesvalue = true; --record that it has already been saved return out..'}' end outFuncs'table' = tableOut; local tTable= tableOut(Table); return tableOut(Table); end return p